package main

import (
	"fmt"
	"snmp-server/snmp"
	"strconv"
	"sync"
	"time"
)

func main() {
	s := snmp.NewSnmpConn("192.168.1.59")
	defer s.Close()

	runTalk(
		//MAC
		func(wg *sync.WaitGroup) {
			defer wg.Done()
			ip, err := s.GetMac()
			if err != nil {
				fmt.Println("get ip err:", err)
			} else {
				fmt.Println("ip:", ip)
			}
		},

		//内存大小
		func(wg *sync.WaitGroup) {
			defer wg.Done()
			mem, err := s.GetMemory()
			if err != nil {
				fmt.Println("get mem err:", err)
			} else {
				//单位kb转为mb
				i, _ := strconv.Atoi(mem)
				// fmt.Println("mem:", i>>10, "mb")
				fmt.Println("mem:", float64(i)/1024, "mb")
			}
		},

		//内存使用率
		func(wg *sync.WaitGroup) {
			defer wg.Done()
			mem, err := s.GetMemoryUsage()
			if err != nil {
				fmt.Println("get mem usage err:", err)
			} else {
				fmt.Println("mem usage:", mem)
			}
		},

		//系统运行时间
		//系统运行时间计算系统开机时间
		func(wg *sync.WaitGroup) {
			defer wg.Done()
			sys, err := s.GetSysUpTime()
			if err != nil {
				fmt.Println("get sys up time err:", err)
			} else {
				fmt.Println("sys up time:", sys)
			}

			timestamp, _ := strconv.Atoi(sys)
			sysStartTime := time.Now().Add(-time.Duration(timestamp) * time.Microsecond).Format("2006-01-02 15:04:05")
			fmt.Println("sys start time:", sysStartTime)
		},
	)

	//重启设备
	// res, err := s.SetReboot()
	// if err != nil {
	// 	fmt.Println("set reboot err:", err)
	// } else {
	// 	fmt.Println("set reboot:", res)
	// }
}

func runTalk(f ...func(wg *sync.WaitGroup)) {
	wg := sync.WaitGroup{}
	wg.Add(len(f))
	for _, v := range f {
		go v(&wg)
	}
	wg.Wait()
}
